<?php
if(!defined('IN_PASSPORT')) {
	exit('Access Denied');
}

if(empty($_SERVER["HTTP_USER_AGENT"]) || $_SERVER["HTTP_USER_AGENT"] == "Mozilla/4.0") {
	exit('Access Denied');
}
			
	$strMd5Auth		= md5($strAuth.$passport_key);
	if($strMd5Auth != $strVerify) {
		$result = -11;			//无效的参数！
		$rurl = $rurl . "?ecode=".$result;
		echo "<meta http-equiv=refresh content='0; url=".$rurl."'>";
		exit;
	}
			$strUserAccount = isset($arrData["account"]) ? trim($arrData["account"]) : "";
			$strPwdMd5		= isset($arrData["pwd"]) ? $arrData["pwd"] : "";
			$strClientIp	= isset($arrData["ip"]) ? $arrData["ip"] : "";
			$strGameClientIp = get_client_ip();
			$iAdultFlag		= isset($arrData["adultFlag"]) ? $arrData["adultFlag"] : 0;
			$iAdultGameTime	= isset($arrData["adultGameTime"]) ? $arrData["adultGameTime"] : 0;
			$strTime		= isset($arrData["time"]) ? strtotime($arrData["time"]) : 0;
			$strAdSource	= isset($arrData["ad_source"]) ? trim($arrData["ad_source"]) : "";
			$strAdPos		= isset($arrData["ad_pos"]) ? trim($arrData["ad_pos"]) : "";

			$strMd5Auth		= md5($strAuth.$passport_key);

			$result = 1;
			if($strMd5Auth != $strVerify) {
				$result = -11;			//无效的参数！
			} else if($strTime == 0) {
				$result = -12;			//无效的参数！
			} else if(empty($strUserAccount)) {
				$result = -13;			//无效的参数！
			} else if(time() - $strTime > $timeExpire) {
				$result = -14;			//时间过期
			}
			

			if($result != 1) {
				$rurl = $rurl . "?ecode=".$result;
				echo "<meta http-equiv=refresh content='0; url=".$rurl."'>";
			} else {
				require(INCLUDE_PATH . "/common/session.class.php");
				require(INCLUDE_PATH . "/player/account.class.php");
				require(INCLUDE_PATH . "/player/player.class.php");
				require(INCLUDE_PATH . "/online/online.class.php");
				
				$objRd		= new php_redis(RD_MASTER_HOST,RD_MASTER_PORT,RD_DB_NAME);
				
				$objSession = new Session(& $objRd);
				$objAccount = new account();
				$objPlayer	= new player();
				$objOnline	= new online();
				$objAccount->set_rd(& $objRd);
				$objPlayer->set_rd(& $objRd);
				$objOnline->set_rd(& $objRd);
				
				$sid = session_id();

				$arrAccount = $objAccount->get($strUserAccount);

				/*-------------------------[ delete before sid ]--------------------[ begin ]---*/
				if(!empty($arrAccount["sid"]) && $arrAccount["sid"] != $sid) {
					$arrOldSession = $objSession->get_session_data($arrAccount["sid"]);
					if($arrOldSession) {
						$bResult = $objSession->del_session_data($arrAccount["sid"]);
						
						if(isset($arrOldSession["p_id"])) {
							if(isset($arrOldSession["city"])) {
								$bResult = $objOnline->del_online_list($arrOldSession["city"],$arrOldSession["p_id"]);
							}
						}
					}
				}
				if(isset($_SESSION["account"]) && $_SESSION["account"] != $strUserAccount) {
					if(isset($_SESSION["p_id"])) {
						if(isset($_SESSION["city"])) {
							$bResult = $objOnline->del_online_list($_SESSION["city"],$_SESSION["p_id"]);
						}
					}
				}
				/*-------------------------[ delete before sid ]--------------------[ end ]---*/
				if(!$arrAccount) {
					/*
					if(strpos($strGameClientIp, "192.168.0") === false && $strGameClientIp != "116.226.228.64") {
						url_msg_redirect("帐号不存在！","/login_test.php");
						exit;
					}
					*/

					$bResult = $objAccount->add($strUserAccount,time(),0,$strAdSource,$strAdPos,$sid);

					if(!$bResult) {
						$rurl = $rurl . "?ecode=-15";
						echo "<meta http-equiv=refresh content='0; url=".$rurl."'>";
						exit;
					}
					$bResult = $objAccount->add_gold($strUserAccount,5000);
				} else {
					$bResult = $objAccount->set_account_sid($strUserAccount,$sid);
				}

				sessionset("account",$strUserAccount);
				
				session_write_close();

				unset($objRd,$objAccount,$objPlayer);

				echo "<meta http-equiv=refresh content='0; url=/index.php'>";
			}

			
?>